package cn.yougou.controller;


import cn.yougou.common.DtoUtil;
import cn.yougou.dto.Dto;
import cn.yougou.pojo.YgCategory;
import cn.yougou.service.admin.YgAdminCategoryService;
import cn.yougou.vo.YgCategoryVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

/**
 * (YgAdminCommodity)表控制层
 *
 * @author 白杰崇
 * @since 2020-12-03 01:32:19
 */
@RestController
@RequestMapping("ygAdminCategory")
public class YgAdminCategoryController {

    /**
     * 后台管理商品模块
     */
    @Reference
    private YgAdminCategoryService ygAdminCategoryService;

    /**
     * 查询全部类目
     * @return
     */
    @GetMapping("/category")
    public Dto getCategory() {
        Dto dto = null;
        try {
            //分类页面默认 首先查询一级类目，传参为0
            YgCategory ygCategory = new YgCategory(0);
            List<YgCategoryVo> allList =  ygAdminCategoryService.categoryAll(ygCategory);
            dto = DtoUtil.returnSuccess("查询成功",allList);
        }catch (Exception e) {
            e.printStackTrace();
            dto = DtoUtil.returnFail("系统错误！","49999");
        }
        return dto;
    }



    /**
     * 查询所有一级类目
     * @return
     */
    @GetMapping("/category/l1")
    public Dto getCatL1() {
        List<YgCategoryVo> list = null;
        try {
            YgCategory ygCategory = new YgCategory();
            ygCategory.setLevel("L1");
            list = ygAdminCategoryService.categoryByCondition(ygCategory);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 查询所有二级类目
     * @return
     */
    @GetMapping("/category/l2")
    public Dto getCatL2() {
        List<YgCategoryVo> list = null;
        try {
            YgCategory ygCategory = new YgCategory();
            ygCategory.setLevel("L2");
            list = ygAdminCategoryService.categoryByCondition(ygCategory);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }


    /**
     * 新增类目
     * @return
     */
    @PostMapping("/creationCategory")
    public Dto creation(@RequestBody YgCategory ygCategory){
        if(ygCategory.getPid() == null){
            return DtoUtil.returnFail("缺少必要的参数！","50001");
        }
        ygCategory.setSortorder(null);
        ygCategory.setAddtime(new Date());
        ygCategory.setDeleted(1);
        ygCategory.setUpdatetime(null);
        int updateRow = ygAdminCategoryService.creation(ygCategory);
        return DtoUtil.returnDataSuccess(updateRow);
    }

    /**
     * 修改类目
     * @return
     */
    @PostMapping("/updateCategory")
    public Dto updateCategory(@RequestBody YgCategory ygCategory){
        return DtoUtil.returnDataSuccess(
                ygAdminCategoryService.update(ygCategory)
        );
    }

    /**
     * 删除类目(逻辑删除)
     * @return
     */
    @PostMapping("/deleteCategory")
    public Dto deleteCategory(@RequestBody YgCategory ygCategory){
        if(ygCategory.getPid() == null){
            return DtoUtil.returnFail("缺少必要的参数！","50001");
        }
        ygCategory.setDeleted(0);
        return DtoUtil.returnDataSuccess(
                ygAdminCategoryService.delete(ygCategory)
        );
    }


}
